﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Microsoft.AspNetCore.Http;
using XW.Core;

namespace XW.Utils
{
    public static partial class CommonHelper
    {
        public static string GetRequest(string key)
        {
            try
            {

                string value = "";
                if (SXHttpContext.Current.Request.Query.ContainsKey(key))
                {
                    value = SXHttpContext.Current.Request.Query[key];
                }
                if (SXHttpContext.Current.Request.Method.ToUpper() == "POST")
                {
                    if (SXHttpContext.Current.Request.Form.ContainsKey(key))
                    {
                        value = SXHttpContext.Current.Request.Form[key];
                    }
                }

                return value;
            }
            catch (Exception)
            {

                return "";
            }


        }
        public static int GetPageIndex()
        {
            return CommonHelper.ToInt(GetRequest("pageIndex"), 0);
        }

        public static int GetPageRows()
        {
            return CommonHelper.ToInt(GetRequest("pageSize"), 20);
        }

        public static string GetSort(string defaultSort)
        {
            string sort = GetRequest("sortField");
            if (sort.IsNullOrEmpty())
            {
                return defaultSort;
            }
            string order = GetRequest("sortOrder");
            if (order.IsNullOrEmpty())
            {
                order = "asc";
            }
            return sort + " " + order;
        }
        public static string GetSort4Mg(string defaultSort)
        {
            string sort = GetRequest("sortField");
            if (sort.IsNullOrEmpty())
            {
                return defaultSort;
            }
            string order = GetRequest("sortOrder");
            if (order.IsNullOrEmpty())
            {
                order = "asc";
            }
            return sort + ":" + (order == "asc" ? "1" : "-1");
        }


        public static PageModel GetPageModel(string defaultSort ="", string defaultSortType="")
        {
            PageModel page;

            var body = GetBody();
            page = JsonHelper.ToObject<PageModel>(body);
            page = page == null ? new PageModel() : page;
            page.Page = page.Page > 0 ? page.Page : 1;
            page.Limit = page.Limit > 0 ? page.Limit : 20;
            if (page.SortField.IsNullOrEmpty())
            {
                page.SortField = defaultSort;
            }
            if (page.SortType.IsNullOrEmpty())
            {
                page.SortType = defaultSortType;
            }
            return page;
        }
        public static T GetPageModel<T>(string defaultSortField = "", string defaultSortType = "") where T : PageModel, new()
        {
            T page = new T();
            if (SXHttpContext.Current.Request.HasFormContentType)
            {
                page = GetPageModel4Form<T>();
            }
            else
            {
                var body = GetBody();
                page = JsonHelper.ToObject<T>(body);
                if (page == null)
                {
                    page = new T();
                }
                page.Page = page.Page > 0 ? page.Page : 1;
                page.Limit = page.Limit > 0 ? page.Limit : 20;
                if (page.SortField.IsNullOrEmpty())
                {
                    page.SortField = defaultSortField;
                }
                if (page.SortType.IsNullOrEmpty())
                {
                    page.SortType = defaultSortType;
                }
            }
            return page;
        }
        /// <summary>
        ///  从Form 获取  PageModel
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="defaultSortField"></param>
        /// <param name="defaultSortType"></param>
        /// <returns></returns>
        public static T GetPageModel4Form<T>(bool needTrim = false,string defaultSortField = "", string defaultSortType = "") where T : PageModel, new()
        {
            

            var request = SXHttpContext.Current.Request;
            var type = typeof(T);
            var ps = ReflectionHelper.GetProperties(type);

            T page = CommonHelper.Dic2Object<T>(request.Form);
             
            page = page == null ? new T() : page;
            page.Page = page.Page > 0 ? page.Page : 1;
            page.Limit = page.Limit > 0 ? page.Limit : 20; 
            if (page.SortField.IsNullOrEmpty())
            {
                page.SortField = defaultSortField;
            }
            if (page.SortType.IsNullOrEmpty())
            {
                page.SortType = defaultSortType;
            } 

            return page;
        }



        public static string GetBody()
        { 
            var body = "";
            var request =  SXHttpContext.Current.Request;
            request.EnableBuffering();
            request.Body.Seek(0, 0);
            using (var inputStream = new MemoryStream()) 
            using (var reader = new StreamReader(inputStream))
            {
                request.Body.CopyTo(inputStream);
                inputStream.Position = 0;
                //  SXHttpContext.Current.Request.Body.Position = 0;
                body = reader.ReadToEndAsync().Result;
            } 
            return body;
        }
        /// <summary>
        /// 获取用户Id
        /// </summary>
        /// <returns></returns>
        public static long GetUserId() {
            if ( SXHttpContext.Current != null && SXHttpContext.Current.User != null)
            {
               return CommonHelper.ToInt64( SXHttpContext.Current.User.FindFirstValue(JwtTokenConst.JWT_USER_ID));
            }
            return 0;
        }
    }

    public class PageModel
    {
        /// <summary>
        /// 关键字
        /// </summary>
        public string Keyword { get; set; }
        /// <summary>
        /// 页码 1 开始
        /// </summary>
        public int Page { get; set; }

        /// <summary>
        /// 每页条数
        /// </summary>
        public int Limit { get; set; }

        /// <summary>
        /// 排序 字段
        /// </summary>
        public string SortField { get; set; }

        /// <summary>
        /// 排序 类型
        /// </summary>
        public string SortType { get; set; }

    } 
}
